<?php
namespace app\index\controller;

use think\Cookie;

class Index extends Common
{
    public function islogin()
    {
        if($uid = Cookie::get("smalls_userId"))
        {
            $sid =Cookie::get("smalls_userSid");
            $row = $this->pdo->find("select * from smalls_user where uid=:uid limit 1", array(':uid' => $uid));
            $check = getPwd( 'www.16145.com' . $row['pwd']);
            if($sid === $check){
                return true;
            }
        }
        $this->assign('sweetAlert',getUrl(url('user/login')));
        exit($this->fetch('common/sweetalert'));
    }
    public function index()
    {
        //判断是否登录
        $this->islogin();
        //计算用户数量
        $amount['user'] = $this->pdo->getCount("select uid from pre_users");
        $amount['juser'] = $this->pdo->getCount("select uid from pre_users where TO_DAYS(regtime) = TO_DAYS(NOW())");
        //加载首页模板
        $this->assign( 'webTitle', '接口授权控制面板' );
        $this->assign( 'login_title', '接口授权管理员平台' );
        $this->assign( 'login_footer', '巨玩网络科技有限公司' );
        return $this->fetch();
    }
    public function docking(){
        //判断是否登录
        $this->islogin();
        //加载用户信息
        $uid = Cookie::get("smalls_userId");
        $row = $this->pdo->find("select * from smalls_user where uid=:uid limit 1", array(':uid' => $uid));
        $money = $row['money'];
        $this->assign( 'money', $money);
        //加载首页模板
        $this->assign( 'webTitle', '接口授权控制面板' );
        $this->assign( 'login_title', '接口授权管理员平台' );
        $this->assign( 'login_footer', '巨玩网络科技有限公司' );
        return $this->fetch();
    }
    public function validateEmail()
    {
        $sid = input('sid');
        if (strlen($sid) != 32 || !$this->pdo->find('select * from smalls_user where status=:status and sid=:sid limit 1', array(':status' => 0,':sid' => $sid))) {
            $this->assign('sweetAlert',sweetAlert('链接失效','链接失效或非法行为！','2',url('user/reg')));
        } else {
            $this->pdo->execute("update smalls_user set status=:status where sid=:sid limit 1", array(':status' => '1', ':sid' => $sid));
            $this->assign('sweetAlert',sweetAlert('激活成功','正在为你转跳到登录页面','1',url('user/login')));
        }
        exit($this->fetch('common/sweetalert'));
    }
    public function sendValidateEmail()
    {
        $email = input('email');
        if (!$email || !$row = $this->pdo->find('select * from smalls_user where status=:status and mail=:mail limit 1', array(':status' => 0,':mail' => $email))) {
            $this->assign('sweetAlert',sweetAlert('用户不存在','用户不存在或已经激活！','2',url('user/login')));
        } else {
            $url = "http://{$_SERVER['HTTP_HOST']}" . url('index/validateEmail', ['sid' => $row['sid']]);
            $body = "尊敬的用户：<br>您好，<br>请点击下面链接进行邮箱验证：<a href='{$url}' target='_blank'>{$url}</a><br>邮箱验证后，你就可以开始使用平台了！";
            $mail = sendEmail($email, config('web_name') . "-邮箱验证", $body);
                if ($mail->send()) {
                    $this->assign('sweetAlert',sweetAlert('发送成功','请先登录邮箱进行激活账号','1',url('user/login')));
                } else {
                     $this->assign('sweetAlert',sweetAlert('发送失败','邮件发送失败：' . $mail->ErrorInfo,'2','REFERER'));
                 }
        }
        exit($this->fetch('common/sweetalert'));
    }
    public function resetPassword()
    {
        $sid = input('sid');
        if (strlen($sid) != 32 || !$this->pdo->find('select * from smalls_user where status=:status and sid=:sid limit 1', array(':status' => 1,':sid' => $sid))){
            $this->assign('sweetAlert',sweetAlert('链接失效','链接失效或非法行为！','2',url('user/password')));
        } else {
            $this->pdo->execute("update smalls_user set pwd=:pwd where sid=:sid limit 1", array(':pwd' => getPwd('123456'), ':sid' => $sid));
            $this->assign('sweetAlert',sweetAlert('重置成功','密码已经重置成123456','1',url('user/login')));
        }
        exit($this->fetch('common/sweetalert'));
    }
}
